# cython: language_level=3
#!/usr/bin/python
# -- coding:utf8 --
import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import app.model_phl.util_log as utils
from app.model_phl.utils import select_sql_mysql_v1
import warnings

warnings.filterwarnings("ignore")


def get_age_data(ApplyNO):
    """
    从MySQL获取年龄数据
    :param ApplyNO:
    :return:
    """
    age_sql = f"""
    select distinct 
    UserId
    ,if(Birthday is null, -9999, TIMESTAMPDIFF(YEAR, Birthday, CURDATE())) as age 
    from SysCardAuthentication
    where UserId = (
    select distinct 
    UserId
    from SysJkApply
    where Isrenew = 0
    and ApplyNO = '{ApplyNO}' 
    )
    """
    age_data = select_sql_mysql_v1(age_sql)
    return age_data


def get_userinfo_freatures(ApplyNO):
    """
    用户基本信息数据的特征集
    :param ApplyNO:
    :return:
    """
    try:
        user_df = get_age_data(ApplyNO)
        if user_df.empty:
            return {"age": -9999}
        age = user_df.loc[0, "age"]

        return {
            "age": age,
        }
    except Exception as e:
        utils.get_logger().error(e)
        return {
            "age": -9976,
        }


if __name__ == "__main__":
    # 测试用例：168085352166035296
    ApplyNO = "168085352166035296"
    print(get_userinfo_freatures(ApplyNO))
